package com.yupi.mianshiya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yupi.mianshiya.constant.CommonConstant;
import com.yupi.mianshiya.model.dto.learningPath.LearningPathQueryRequest;
import com.yupi.mianshiya.model.entity.LearningPath;
import com.yupi.mianshiya.service.LearningPathService;
import com.yupi.mianshiya.mapper.LearningPathMapper;
import com.yupi.mianshiya.utils.SqlUtils;
import org.springframework.stereotype.Service;


@Service
public class LearningPathServiceImpl extends ServiceImpl<LearningPathMapper, LearningPath>
    implements LearningPathService{

    @Override
    public QueryWrapper<LearningPath> getQueryWrapper(LearningPathQueryRequest learningPathQueryRequest) {
        QueryWrapper<LearningPath> queryWrapper = new QueryWrapper<>();
        if (learningPathQueryRequest==null){
            return queryWrapper;
        }
        Integer id = learningPathQueryRequest.getId();
        String title = learningPathQueryRequest.getTitle();
        Long userId = learningPathQueryRequest.getUserId();


        String sortField = learningPathQueryRequest.getSortField();
        String sortOrder = learningPathQueryRequest.getSortOrder();

        queryWrapper.eq(id!=null, "id", id);
        queryWrapper.like(title!=null, "title", title);
        queryWrapper.eq(userId!=null, "userid", userId);

        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }
}




